//
//  RCMerchantDetailMessageCell.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-9-12.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCMerchantDetailMessageCell.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "NSString+NSString_Ex_.h"
#import "TTTAttributedLabel.h"
#import "NSAttributedString+LVAddition.h"
#import "RCMerchantDetailSigninDataModel.h"

#define kAnchorImageFile            @"image_merchant_mesage_cell_anchor.png"
#define kBGImageFile          @"image_merchant_message_cell_bg.png"
#define kStartActiveImageFile    @"image_start_active.png"
#define kStartInActiveImageFile  @"image_merchant_inactive.png"

@interface RCMerchantDetailMessageCell()

@property(nonatomic,retain)UIImageView *backgroundImageView;
@property(nonatomic,retain)UIImageView *anchorImageView;
@property(nonatomic,retain)UIImageView *startImageView1;
@property(nonatomic,retain)UIImageView *startImageView2;
@property(nonatomic,retain)UIImageView *startImageView3;
@property(nonatomic,retain)UIImageView *startImageView4;
@property(nonatomic,retain)UIImageView *startImageView5;
@property(nonatomic,retain)UILabel     *userNameLabel;
@property(nonatomic,retain)TTTAttributedLabel *contentLabel;
@property(nonatomic,retain)UILabel *timeLabel;


@end

@implementation RCMerchantDetailMessageCell
@synthesize backgroundImageView = _backgroundImageView;
@synthesize anchorImageView = _anchorImageView;
@synthesize startImageView1 = _startImageView1;
@synthesize startImageView2 = _startImageView2;
@synthesize startImageView3 = _startImageView3;
@synthesize startImageView4 = _startImageView4;
@synthesize startImageView5 = _startImageView5;
@synthesize userNameLabel = _userNameLabel;
@synthesize contentLabel = _contentLabel;
@synthesize timeLabel = _timeLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self createPlaceHolderView];
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)createPlaceHolderView{
    _backgroundImageView = [[UIImageView alloc]init];
    _backgroundImageView.backgroundColor = [UIColor clearColor];
    UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]];
    bgImage = [bgImage resizableImageWithCapInsets:UIEdgeInsetsMake(40, 120, 40, 120)];
    [_backgroundImageView setImage:bgImage];
    [self addSubview:_backgroundImageView];
    
    _startImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 11, 14, 14)];
    _startImageView1.backgroundColor = [UIColor clearColor];
    [_backgroundImageView addSubview:_startImageView1];
    
    _startImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(25, 11, 14, 14)];
    _startImageView2.backgroundColor = [UIColor clearColor];
    [_backgroundImageView addSubview:_startImageView2];
    
    _startImageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(40, 11, 14, 14)];
    _startImageView3.backgroundColor = [UIColor clearColor];
    [_backgroundImageView addSubview:_startImageView3];
    
    _startImageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(55, 11, 14, 14)];
    _startImageView4.backgroundColor = [UIColor clearColor];
    [_backgroundImageView addSubview:_startImageView4];
    
    _startImageView5 = [[UIImageView alloc]initWithFrame:CGRectMake(70, 11, 14, 14)];
    _startImageView5.backgroundColor = [UIColor clearColor];
    [_backgroundImageView addSubview:_startImageView5];
    
    
    _contentLabel = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(10, 27, 280, 100)];
    _contentLabel.backgroundColor = [UIColor clearColor];
    _contentLabel.textColor = [UIColor colorWithHex:0xff505050];
    _contentLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
    _contentLabel.numberOfLines = 0;
    _contentLabel.leading = 6;
    [_backgroundImageView addSubview:_contentLabel];
    
    _userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 137, 250, 13)];
    _userNameLabel.backgroundColor = [UIColor clearColor];
    _userNameLabel.textColor = [UIColor colorWithHex:0xff989797];
    _userNameLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    [_backgroundImageView addSubview:_userNameLabel];
    
    
    _timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 152, 200, 13)];
    _timeLabel.backgroundColor = [UIColor clearColor];
    _timeLabel.textColor = [UIColor colorWithHex:0xff98979797];
    _timeLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
    [_backgroundImageView addSubview:_timeLabel];
    
    _anchorImageView = [[UIImageView alloc]init];
    _anchorImageView.backgroundColor = [UIColor clearColor];
    [_anchorImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kAnchorImageFile ofType:nil]]];
    [self addSubview:_anchorImageView];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
}

-(void)bindData:(RCMerchantDetailSigninItem *)data withIsFirstCell:(BOOL)isFirst{
    if (nil == data) {
        return;
    }
    if (isFirst) {
        _anchorImageView.frame = CGRectMake(150, 9, 19, 18);
    }else{
        _anchorImageView.frame = CGRectMake(150, 6, 19, 18);
    }
    
    NSInteger score = data.score;
    NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
    UIImage *activeImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartActiveImageFile]];
    UIImage *inactiveImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartInActiveImageFile]];
    [_startImageView1 setImage:score > 0 ? activeImage : inactiveImage];
    [_startImageView2 setImage:score > 1 ? activeImage : inactiveImage];
    [_startImageView3 setImage:score > 2 ? activeImage : inactiveImage];
    [_startImageView4 setImage:score > 3 ? activeImage : inactiveImage];
    [_startImageView5 setImage:score > 4 ? activeImage : inactiveImage];
    
    _contentLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:data.content]) {
        _contentLabel.hidden = NO;
        _contentLabel.text = data.content;
        CGSize size = [_contentLabel.attributedText sizeWithConstrainedToSize:CGSizeMake(280, 2000) Leading:6];
        CGRect rect = _contentLabel.frame;
        rect.size.height = size.height;
        _contentLabel.frame = rect;
    }
    
    _userNameLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:data.userName]) {
        _userNameLabel.hidden = NO;
        _userNameLabel.text = data.userName;
        CGRect rect = _userNameLabel.frame;
        rect.origin.y = CGRectGetMaxY(_contentLabel.frame)+10;
        _userNameLabel.frame = rect;
    }
    
    _timeLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:data.dateTime]) {
        _timeLabel.hidden = NO;
        _timeLabel.text = data.dateTime;
        CGRect rect = _timeLabel.frame;
        rect.origin.y = CGRectGetMaxY(_userNameLabel.frame)+2;
        _timeLabel.frame = rect;
    }
    
    CGFloat height = CGRectGetMaxY(_timeLabel.frame)+10;
    if (isFirst) {
        _backgroundImageView.frame = CGRectMake(10, 18, 300, height);
    }else{
        _backgroundImageView.frame = CGRectMake(10, 15, 300, height);
    }
}

+(CGFloat)getCellHeight:(RCMerchantDetailSigninItem *)data withIsFirstCell:(BOOL)isFirst{
    if (nil == data) {
        return 0.0f;
    }
    CGFloat height = 0;
    if (isFirst) {
        height =18+11+14;
    }else{
        height =15+11+14;
    }
    if (![NSString stringIsNullOrEmpty:data.content]) {
        TTTAttributedLabel *label = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(10, 27, 280, 100)];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor colorWithHex:0xff505050];
        label.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
        label.numberOfLines = 0;
        label.leading = 6;
        label.text = data.content;
        CGSize size = [label.attributedText sizeWithConstrainedToSize:CGSizeMake(280, 2000) Leading:6];
        height = height +2 + size.height;
        [label release];
    }
    
    if (![NSString stringIsNullOrEmpty:data.userName]) {
        height +=23;
    }
    if (![NSString stringIsNullOrEmpty:data.dateTime]) {
        height +=15;
    }
    height +=10;
    return height;
}

@end
